package com.ruoyi.mongodb.controller;

import com.ruoyi.mongodb.domain.Hero;
import com.ruoyi.mongodb.domain.ResponseEntity;
import com.ruoyi.mongodb.mapper.HeroMapper;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 韩国伟
 * @version 1.0
 * @description: 测试类
 * @date 2023/2/28 20:20
 */
@RestController
@RequestMapping("/her")
public class HeroController {


    @Resource
    private HeroMapper heroMapper;

    @Resource
    private MongoTemplate mongoTemplate;

//    @RequestMapping("/list")
//    public ResponseEntity list(){
//        List<Hero> list=heroMapper.findAll();
//        return new ResponseEntity(list);
//    }

    /*
    * 添加
    * */

    @PostMapping("/add")
    public ResponseEntity add(@RequestBody Hero hero){
        heroMapper.save(hero);
        return new ResponseEntity("添加成功");
    }


    /*
    *   修改
    *
    * */

    @RequestMapping("/update")
    public ResponseEntity update(@RequestBody Hero hero){
        heroMapper.save(hero);
        return new ResponseEntity("修改成功");
    }

    /*
    * 删除
    * */


    @RequestMapping("/del")
    public ResponseEntity del(@RequestBody Hero hero){
        heroMapper.delete(hero);
        return new ResponseEntity("删除成功");
    }

    /*
    *
    * 分页查询
    * */
    @RequestMapping("/find/{page}/{size}")
    public List<Hero> findByPage(@PathVariable Integer page,@PathVariable Integer size) {
        Query query = new Query();
        Integer indedx=(page-1)*size;
        Pageable pageable= PageRequest.of(indedx,size);
        query.with(pageable);
        List<Hero> list=mongoTemplate.find(query,Hero.class);
        return  list;
    }

}
